package com.chenbei.controller.admin.common;

import com.chenbei.common.domain.Areas;
import com.chenbei.common.domain.vo.ApiResponse;
import com.chenbei.common.domain.vo.TreeNode;
import com.chenbei.common.service.IAreasService;
import com.chenbei.common.util.ApiResponseUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "省市区管理")
@RestController("AdminAreas")
@RequestMapping("/admin/common/areas")
public class AreasController {

    @Autowired
    private IAreasService areasService;

    @Operation(summary = "初始化或更新")
    @GetMapping("initOrUpdate")
    public ApiResponse<Object> initOrUpdate() {
        areasService.init();
        return ApiResponseUtil.success();
    }

    @Operation(summary = "获取父节点下节点列表")
    @GetMapping("listByPid")
    public ApiResponse<List<Areas>> listByPid(@RequestParam(value = "pid", defaultValue = "0") long pid) {
        List<Areas> areasList = areasService.listByPid(pid);
        return ApiResponseUtil.success(areasList);
    }

    @Operation(summary = "获取子节点下的所有节点")
    @GetMapping("treeByPid")
    public ApiResponse<List<TreeNode<Areas>>> treeByPid(@RequestParam(value = "pid", defaultValue = "0") long pid){
        List<TreeNode<Areas>> tree = areasService.tree(pid);
        return ApiResponseUtil.success(tree);
    }

    @Operation(summary = "获取地区树形结构")
    @GetMapping("tree")
    public ApiResponse<List<TreeNode<Areas>>> tree(@RequestParam(defaultValue = "0") Long pid) {
        List<TreeNode<Areas>> tree = areasService.tree(pid);
        return ApiResponseUtil.success(tree);
    }

    @Operation(summary = "获取省市地区树形结构")
    @GetMapping("provinceCityTree")
    public ApiResponse<List<TreeNode<Areas>>> provinceCityTree() {
        List<TreeNode<Areas>> tree = areasService.provinceCityTree();
        return ApiResponseUtil.success(tree);
    }
}
